home *** CD-ROM | disk | FTP | other *** search
- Path: li.net!sbda
- From: sbda@newshost.li.net (sbda)
- Newsgroups: comp.lang.c
- Subject: Re: How to tell if a file exists in C
- Date: 17 Feb 1996 02:11:51 GMT
- Organization: LI Net (Long Island Network)
- Message-ID: <4g3dh7$co6@linet06.li.net>
- References: <823685019.AA00170@escan.demon.co.uk>
- NNTP-Posting-Host: linet01.li.net
- X-Newsreader: TIN [version 1.2 PL2]
-
- Bill Birrell (bill@escan.demon.co.uk) wrote:
- : > Hi, how do I find out if a file already exists
- : > in UNIX C? On PCs I would do a findfirst/findnext,
- : > is there an equivalent on Unix?
-
- : Check up on access() and stat(). There cannot be an exact equivalent to
- : Digital Research's findfirst and findnext functions in unix, because unix has
- : a completely different file structure from CP/M or PC-Dos (later MsDos), and
- : therefore has no idea what FCBs are. Files, directories and devices are all
- : accessed the same way, and when you become used to it, it is a *much* simpler
- : and more logical approach. Take a look at K&R Chapter 8 [Either edition - it's
- : in both].
-
- : Bill.
-
-
- : --
- : Regards,
- : Bill Birrell.
- : internet: bill@escan.demon.co.uk
-
-
- Here's another way - it's ANSI C - so it should work on all C compilers
- and on all OSes:
-
- /*---------------------------------------*/
- FILE *fp;
-
- fp = fopen("filename.ext", "r");
-
- if (fp == NULL) puts("File not found");
- /*---------------------------------------*/
-
- If the filespec doesn't exist, fp will be set to NULL. If it does
- exist, then fp will contain a non-NULL (non-zero) value.
-
-